package com.reservation.service.impl;

import com.reservation.entity.Reservation;
import com.reservation.mapper.ReservationMapper;
import com.reservation.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {
    
    @Autowired
    private ReservationMapper reservationMapper;
    
    @Override
    public List<Reservation> findAll() {
        return reservationMapper.findAll();
    }
    
    @Override
    public List<Reservation> findByUserId(Integer userId) {
        return reservationMapper.findByUserId(userId);
    }
    
    @Override
    public Reservation findById(Integer id) {
        return reservationMapper.findById(id);
    }
    
    @Override
    public void save(Reservation reservation) {
        // 设置默认状态为待审核
        if (reservation.getStatus() == null) {
            reservation.setStatus("pending");
        }
        reservationMapper.save(reservation);
    }
    
    @Override
    public void update(Reservation reservation) {
        reservationMapper.update(reservation);
    }
    
    @Override
    public void deleteById(Integer id) {
        reservationMapper.deleteById(id);
    }
    
    @Override
    public void updateStatus(Integer id, String status) {
        reservationMapper.updateStatus(id, status);
    }
    
    @Override
    public List<Reservation> findByVenueId(Integer venueId) {
        return reservationMapper.findByVenueId(venueId);
    }
    
    @Override
    public boolean checkTimeConflict(Integer venueId, String startTime, String endTime) {
        List<Reservation> conflicts = reservationMapper.checkTimeConflict(venueId, startTime, endTime);
        return !conflicts.isEmpty();
    }
    
    @Override
    public List<Reservation> search(String keyword, String status) {
        return reservationMapper.search(keyword, status);
    }
} 